define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'adminlte'], function ($, undefined, Backend, Table, Form, Adminlte) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dormitory/statistics/index',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'building.id',
                sortOrder: 'desc',
                search: false,
                commonSearch: false,
                searchFormVisible: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [

                        {field: 'state', checkbox: true, },
                        {field: 'garden_name', title: __('园区'), operate: false},
                        {field: 'name', title: __('楼栋'), operate: false},
                        {field: 'total_count', title: __('总床位'), operate: false},
                        {field: 'used_count', title: __('入住人数'), operate: false},
                        {field: 'used_boy_count', title: __('入住男生数'), operate: false},
                        {field: 'used_girl_count', title: __('入住女生数'), operate: false},
                        {field: 'free_count', title: __('空闲床位数'), operate: false},
                        {field: 'free_boy_count', title: __('空闲男生床位数'), operate: false},
                        {field: 'free_girl_count', title: __('空闲女生床位数'), operate: false},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, buttons: [
                            {
                                name: 'view',
                                title: '查看',
                                text: '查看',
                                icon: '',
                                classname: 'btn btn-success btn-xs btn-click',
                                click: function (e, data) {
                                    Backend.api.open('dormitory/statistics/building/ids/' + data.id, __('楼栋详情'), {area: ['70%', '80%']});
                                }
                            }
                        ], formatter: Table.api.formatter.operate
                        }
                    ]
                ],


            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        building: function () {
            $(document).on('click', '.dorm[data-id]', function () {
                Backend.api.open('dormitory/statistics/dorm/ids/' + $(this).data('id'), __('寝室详情'));
            });
        },
        dorm: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'dormitory/statistics/dorm/ids/' + Config.ids,
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                sortOrder: 'desc',
                search: false,
                commonSearch: false,
                searchFormVisible: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'number', title: __('床位'), operate: false, formatter: Controller.api.formatter.number},
                        {field: 'dorm_number', title: __('寝室号'), operate: false},
                        {field: 'student_number', title: __('学号'), operate: false},
                        {field: 'student_name', title: __('姓名'), operate: false},
                        {field: 'status_text', title: __('状态'), operate: false, formatter: Controller.api.formatter.status},
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                number: function (value, row, index) {
                    return row.dorm_number + ' | ' + row.num + '号床';
                },
                status: function (value, row, index) {
                    if (row.status == 2) {
                        return '在用';
                    } else {
                        return '闲置';
                    }
                }
            },
        }
    };
    return Controller;
});